// pages/chooseLocation/chooseLocation.js
let QQMapWX = require('../../libs/qqmap-wx-jssdk')
let qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    myLat: 30.55396,
    myLon: 114.316,
    centerLat: 30.55396,
    centerLon: 114.316,
    centerAdress:"",
    myAddress:'',
    isEnd: "false",
    markers:[

    ]
  },

  submit(){
    let page = getCurrentPages()
    let prepage = page[page.length - 2]
    if(this.data.isEnd == "true"){
      prepage.setData({
        "endAddress": this.data.centerAdress,
        "endLon": this.data.centerLon,
        "endLat": this.data.centerLat
      })
    }else{
      prepage.setData({
        "startAddress": this.data.centerAdress,
        "startLon": this.data.centerLon,
        "startLat": this.data.centerLat
      })
    }
    wx.navigateBack({
      delta: 1,
    })
  },

  getLocation() {
    console.log("in")
    qqmapsdk = new QQMapWX({
        key: 'RYKBZ-4C36R-CNJWK-W7ND5-HXB77-OXBJI' //之前在腾讯平台申请到的key
    })
    let that = this;

    wx.getLocation({ //获取定位地址经纬度
        type: 'wgs84',
        success(res) {
          console.log(res);
          that.setData({
            "myLat": res.latitude,
            "myLon": res.longitude,
            "centerLat": res.latitude,
            "centerLon": res.longitude,
          })
          console.log("out")
          const latitude = res.latitude
          const longitude = res.longitude
          qqmapsdk.reverseGeocoder({ //SDK调用
            location: {
              latitude,
              longitude
            },
            success: (res) => {
              console.log("return:",res.result.address_component.street_number);
              that.setData({
                "myAddress": res.result.address_component.street_number,
                "centerAdress": res.result.address_component.street_number
              })
            }
          })
        }
    })
},

  chooseLocation(e){
    console.log(e.detail)
    let that = this;
    const longitude = e.detail.longitude;
    const latitude = e.detail.latitude;
    let markers = [{
      id: 1,
      latitude: latitude,
      longitude: longitude,
    }]
    this.setData({
      "centerLat": latitude,
      "centerLon": longitude,
      "markers": markers
    })
    qqmapsdk.reverseGeocoder({ //SDK调用
      location: {
        latitude,
        longitude
      },
      success: (res) => {
        console.log("return:",res.result);
        that.setData({
          "centerAdress": res.result.address_component.street_number,
        })
      }
    })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var isEnd = options.isEnd;
    console.log("in",isEnd);
    this.setData({
      "isEnd": isEnd
    })
    this.getLocation()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})